<template>
  <div class="radarchat">
    <div id="statisticRadarchart" class="echart"></div>
  </div>
</template>

  <script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
echarts.use([PieChart]);
export default {
  name: "radarChartosta",
  props: {
    // 雷达图数据
    radarData: {
      type: Array,
      default: [],
    },
    selectedMode: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    radarData: {
      handler(val) {
        if (val && val.length > 0) {
          this.getChartsdata(this.radarData);
        } else {
          this.chartDes();
        }
      },
      deep: true,
    },
  },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },

    getChartsdata(radarData) {
      let legendName = [];
      let color = [
        "#3639e9",
        "#e97e36",
        "#e936bc",
        "#e93636",
        "#36e954",
        "#b036e9",
        "36cee9",
        "#dde936",
        "#e936d1",
        "#e98d36",
        "#271111",
        "#0954f7",
        "#f70930",
      ];
      radarData.map((item) => {
        legendName.push(item.name);
        return item;
      });
      let option = {
        tooltip: {
          trigger: "item",
        },
        color,
        legend: {
          orient: "vertical",
          top: "20%",
          padding: 0,
          right: "1%",
          icon: "circle",
          itemGap: 3,
          data: legendName,
          textStyle: {
            color: "#8C8C8C",
            fontFamily: "PingFang-SC-Regular, PingFang-SC",
            fontWeight: 400,
            lineHeight: 17,
          },
        },
        series: {
          type: "pie",
          minAngle: 20, //设置扇形的最小占比
          radius: ["40%", "70%"],
          data: radarData,
          selectedMode: this.selectedMode,
          itemStyle: {
            borderColor: "#fff",
            borderWidth: 2,
          },
        },
      };
      this.initChart("statisticRadarchart", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
      this.handleClick();
    },
    handleClick() {
      let that = this;
      that.chart.on("click", function (value) {
        that.$emit("clickPie", value);
      });
    },
  },
};
</script>

<style scoped lang="scss">
.radarchat {
  .echart {
    width: 100%;
    height: 220px;
    padding-right: 5%;
  }
}
</style>
